package all.com.jinyongqunxiax.modules;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;

import net.idik.lib.slimadapter.SlimAdapter;
import net.idik.lib.slimadapter.SlimInjector;
import net.idik.lib.slimadapter.viewinjector.IViewInjector;

import org.apache.commons.io.FilenameUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import all.com.jinyongqunxiax.R;
import all.com.jinyongqunxiax.decoration.EqualSpaceItemDecoration;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class ImageListActivity extends FilterActivity {
    Options     options;
    SlimAdapter adapter;
    List<ViewModel> viewModels       = new ArrayList<>();
    List<ViewModel> filterViewModels = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.image_list_activity);

        options = new Options(getIntent());

        recyclerView = findViewById(R.id.image_list_listview);

        int column = 3;

        // 设置布局管理器
        recyclerView.setLayoutManager(new GridLayoutManager(this, column, RecyclerView.VERTICAL, false));
        // 设置Item添加和移除的动画
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        // 设置Item之间间隔样式
        recyclerView.addItemDecoration(new EqualSpaceItemDecoration(this, column, 10, 10));

        adapter = SlimAdapter.create().register(R.layout.image_list_image_item, new SlimInjector<ViewModel>() {
                    @Override
                    public void onInject(@NonNull ViewModel viewModel, @NonNull IViewInjector injector) {
                        injector.text(R.id.image_list_image_title, viewModel.name);

                        ImageView imageView = (ImageView) injector.findViewById(R.id.image_list_image_view);
                        Glide.with(ImageListActivity.this).load(viewModel.path)
                             .apply(RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.NONE))
                             .apply(RequestOptions.skipMemoryCacheOf(true))
                             .into(imageView);

                        injector.clicked(R.id.image_list_image_view, new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Intent intent = ImageDetailActivity.Options.intent(ImageListActivity.this,
                                        options.mod,
                                        options.component,
                                        viewModel.name,
                                        options.width,
                                        options.height);
                                startActivity(intent);
                            }
                        });
                    }
                }
        ).attachTo(recyclerView);

        setTitle(options.component);
        String location = FilenameUtils.concat(options.mod, options.component);
        File[] files    = new File(location).listFiles();
        for (File file : files) {
            viewModels.add(new ViewModel(file.getName(), file.getPath()));
        }
        adapter.updateData(viewModels);
    }

    @Override
    void filter(String text) {
        text = text.trim().toLowerCase();
        if (text.isEmpty()) {
            adapter.updateData(viewModels);
        } else {
            filterViewModels.clear();

            for (ViewModel viewModel : viewModels) {
                if (viewModel.name.toLowerCase().contains(text)) {
                    filterViewModels.add(viewModel);
                }
            }

            adapter.updateData(filterViewModels);
        }
    }

    public static class Options {
        String mod;
        String component;
        int    width;
        int    height;
        Options(Intent intent) {
            this.mod = intent.getStringExtra("mod");
            this.component = intent.getStringExtra("component");

            this.width = intent.getIntExtra("width", 0);
            this.height = intent.getIntExtra("height", 0);
        }

        static Intent intent(Context context, String mod, String component, int width, int height) {
            Intent intent = new Intent(context, ImageListActivity.class);
            intent.putExtra("width", width);
            intent.putExtra("height", height);
            intent.putExtra("mod", mod);
            intent.putExtra("component", component);
            return intent;
        }
    }

    private static class ViewModel {
        String name;
        String path;
        private Bitmap image;

        ViewModel(String name, String path) {
            this.name = name;
            this.path = path;
        }

        Bitmap getImage() {
            if (image == null) {
                image = BitmapFactory.decodeFile(path);
            }
            return image;
        }
    }
}
